﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using CF.Data.Entity.FrmMemberEntity;

namespace CF.Member.DAL.Member
{
    public class FM_BadgetConfigDAL
    {
        public void Add(FM_BadgeConfig badgeCfg)
        {
            caifendbEntities context = new caifendbEntities();
            badgeCfg.CreateTime = DateTime.Now;
            context.AddToFM_BadgeConfig(badgeCfg);
            context.SaveChanges();
        }

        public List<FM_BadgeConfig> GetAllBadge(int start, int limit)
        {
            List<FM_BadgeConfig> bdlist = new List<FM_BadgeConfig>();
            caifendbEntities context = new caifendbEntities();
            context.ContextOptions.LazyLoadingEnabled = false;
            var query = context.FM_BadgeConfig.OrderBy(c => c.BID).Skip(start).Take(limit);
            bdlist = query.AsQueryable().ToList();
            return bdlist;
        }

        public bool Remove(int bid)
        {
            bool result = false;
            caifendbEntities context = new caifendbEntities();
            FM_BadgeConfig bcfg = context.FM_BadgeConfig.FirstOrDefault(c => c.BID == bid);
            if (bcfg != null)
            {
                context.DeleteObject(bcfg);
                context.SaveChanges();
                result = true;
            }
            return result;
        }

        public bool Update(int bid, FM_BadgeConfig updBadgeCfg)
        {
            bool result = false;
            caifendbEntities context = new caifendbEntities();
            FM_BadgeConfig bdCfg = context.FM_BadgeConfig.FirstOrDefault(c => c.BID == bid);
            if (bdCfg != null)
            {
                bdCfg.BadgeName = updBadgeCfg.BadgeName;
                bdCfg.BadgeImage = updBadgeCfg.BadgeImage;
                bdCfg.BadgeDes = updBadgeCfg.BadgeDes;
                bdCfg.CheckMethod = updBadgeCfg.CheckMethod;
                bdCfg.CheckParam = updBadgeCfg.CheckParam;
                bdCfg.AwardCents = updBadgeCfg.AwardCents;
                bdCfg.AwardExperience = updBadgeCfg.AwardExperience;
                bdCfg.Sort = updBadgeCfg.Sort;
                bdCfg.IsShow = updBadgeCfg.IsShow;
                context.SaveChanges();
                result = true;
            }
            return result;
        }

        public FM_BadgeConfig GetBadge(int bid)
        {
            caifendbEntities context = new caifendbEntities();
            FM_BadgeConfig bcfg = context.FM_BadgeConfig.FirstOrDefault(c => c.BID == bid);
            return bcfg;
        }
    }
}
